package com.pkx.guide.record.service.impl;

import com.pkx.guide.common.util.DateUtils;
import com.pkx.guide.common.util.StringUtils;
import com.pkx.guide.info.entity.TInfoGuide;
import com.pkx.guide.info.entity.TInfoGuidePublish;
import com.pkx.guide.info.service.TInfoGuideService;
import com.pkx.guide.record.entity.TRecordGuideStatus;
import com.pkx.guide.record.mapper.TRecordGuideStatusMapper;
import com.pkx.guide.record.service.TRecordGuideStatusService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * 诱导屏状态检测记录表 服务实现类
 *
 * @author sunsy
 * @since 2023-11-14
 */
@Service
public class TRecordGuideStatusServiceImpl extends ServiceImpl<TRecordGuideStatusMapper, TRecordGuideStatus> implements TRecordGuideStatusService {

    @Resource
    private TInfoGuideService guideService;

    /**
     * 分页列表
     *
     * @param tRecordGuideStatus
     * @return
     */
    @Override
    public IPage<TRecordGuideStatus> pageList(Page page, TRecordGuideStatus tRecordGuideStatus) {
        LambdaQueryWrapper<TRecordGuideStatus> lambdaQueryWrapper = new QueryWrapper<TRecordGuideStatus>().lambda();
        lambdaQueryWrapper.eq(tRecordGuideStatus.getGuideId() != null, TRecordGuideStatus::getGuideId, tRecordGuideStatus.getGuideId());
        lambdaQueryWrapper.ge(StringUtils.isNotEmpty(tRecordGuideStatus.getQueryStartTime()), TRecordGuideStatus::getCreateTime, tRecordGuideStatus.getQueryStartTime());
        lambdaQueryWrapper.le(StringUtils.isNotEmpty(tRecordGuideStatus.getQueryEndTime()), TRecordGuideStatus::getCreateTime, tRecordGuideStatus.getQueryEndTime());
        lambdaQueryWrapper.eq(TRecordGuideStatus::getDelFlg, 0);
        return page(page, lambdaQueryWrapper);
    }

    /**
     * 列表查询
     *
     * @param tRecordGuideStatus
     * @return
     */
    @Override
    public List<TRecordGuideStatus> listByEntity(TRecordGuideStatus tRecordGuideStatus) {
        LambdaQueryWrapper<TRecordGuideStatus> lambdaQueryWrapper = new QueryWrapper<TRecordGuideStatus>().lambda();
        lambdaQueryWrapper.eq(tRecordGuideStatus.getGuideId() != null, TRecordGuideStatus::getGuideId, tRecordGuideStatus.getGuideId());
        lambdaQueryWrapper.ge(StringUtils.isNotEmpty(tRecordGuideStatus.getQueryStartTime()), TRecordGuideStatus::getCreateTime, tRecordGuideStatus.getQueryStartTime());
        lambdaQueryWrapper.le(StringUtils.isNotEmpty(tRecordGuideStatus.getQueryEndTime()), TRecordGuideStatus::getCreateTime, tRecordGuideStatus.getQueryEndTime());
        lambdaQueryWrapper.eq(TRecordGuideStatus::getDelFlg, 0);
        return list(lambdaQueryWrapper);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public TRecordGuideStatus selectById(Integer id) {
        return getById(id);
    }

    /**
     * 保存
     *
     * @param tRecordGuideStatus
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveTRecordGuideStatus(TRecordGuideStatus tRecordGuideStatus) {
        return save(tRecordGuideStatus);
    }

    /**
     * 保存
     *
     * @param tInfoGuidePublish
     * @return
     */
    @Override
    public Boolean saveGuideStatus(TInfoGuidePublish tInfoGuidePublish){
        if (tInfoGuidePublish == null){
            return false;
        }
        String currentDate = DateUtils.getCurrentTimeStr();
        TInfoGuide guideInfo = guideService.selectById(tInfoGuidePublish.getGuideId());
        TRecordGuideStatus tGuiGuideStatus = new TRecordGuideStatus();
        tGuiGuideStatus.setGuideId(tInfoGuidePublish.getGuideId());
        tGuiGuideStatus.setGuidIp(guideInfo != null? guideInfo.getGuidIp(): "");
        tGuiGuideStatus.setOfflineTime(currentDate);
        tGuiGuideStatus.setOfflineStatus(tInfoGuidePublish.getPublishStatus()? "0": "1");
        return save(tGuiGuideStatus);
    }

    /**
     * 修改
     *
     * @param tRecordGuideStatus
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean modify(TRecordGuideStatus tRecordGuideStatus) {
        return updateById(tRecordGuideStatus);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Integer id) {
        if (id == null){
            return false;
        }
        LambdaUpdateWrapper<TRecordGuideStatus> lambdaUpdateWrapper = new UpdateWrapper<TRecordGuideStatus>().lambda()
            .eq(TRecordGuideStatus::getId, id)
            .set(TRecordGuideStatus::getDelFlg, 1);
        return update(lambdaUpdateWrapper);
    }
}
